﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;
using System.Diagnostics;

namespace InsideDL
{
    public partial class frmCIBRMInsUpd : MISControl.frmMatrix
    {
        protected EXLAPP exlAPP = null;
        public frmCIBRMInsUpd()
        {
            InitializeComponent();
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);
            ucIMFile.SelectClick += new EventHandler(getExcelSheetList);
            this.FormClosed += new FormClosedEventHandler(frmCIB001_FormClosed);
            this.controlList = new List<ucSelFileOrPath>
            {
                ucIMFile
            };
        }

        /*
        protected override void execute()
        {
            if (base.Vaildation().Equals(false))
            { return; }

            DataCIBRM rm = new DataCIBRM();
            string sht = null;
            for (int i = 0; i < chkLB.Items.Count; i++)
            {
                if (chkLB.GetItemChecked(i).Equals(true))
                {
                    sht = chkLB.Items[i].ToString();
                    break;
                }
            }
            rm.InsertUpdate(ucIMFile.Txt, sht);            
        }
         */ 

        protected override void execute()
        {
            if (base.Vaildation().Equals(false))
            { return; }

            DataCIBRM rm = new DataCIBRM();
            string sht,execStr,sqlName,bat;
            bat = "TransRM.bat";
            sht = null;
            sqlName = "InsRM";
            execStr = "exec misdb.dbo.myInsertRM '{0}','{1}','{2}'{3}";
            for (int i = 0; i < chkLB.Items.Count; i++)
            {
                if (chkLB.GetItemChecked(i).Equals(true))
                {
                    sht = chkLB.Items[i].ToString();
                    break;
                }
            }
            DataTable dt = rm.GetRMTable(ucIMFile.Txt, sht);
            StringBuilder sb = new StringBuilder();

            foreach (DataRow row in dt.Rows)
            { 
                sb.AppendFormat(execStr,row[0].ToString()
                            ,row[1].ToString()
                            ,row[2].ToString()
                            ,Environment.NewLine
                            );
            }
            string tmp = string.Format("{0}\\{1}.sql", Program.TmpPath(),sqlName);
            Program.CreateFile(tmp, sb.ToString());
            tmp = string.Format("{0}\\{1}", Program.TmpPath(), bat);
            this.createSQLBat(bat,sqlName);
            this.executeBat(tmp);
            /*                        
            createSQLBat();
            */
        }



        protected void getExcelSheetList(object sender, EventArgs e)
        {
            if (exlAPP == null)
            {
                exlAPP = new EXLAPP();
            }

            while (chkLB.Items.Count > 0)
            {
                chkLB.Items.Remove(chkLB.Items[0]);
            }

            exlAPP.OpenFile(ucIMFile.Txt);
            List<string> shtList = exlAPP.GetSheetList();
            foreach (string str in shtList)
            {
                chkLB.Items.Add(str);
            }
            exlAPP.CloseFile();
        }

        void frmCIB001_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (exlAPP != null)
            {
                exlAPP.SaveCloseXLS();
                exlAPP = null;
            }
        }


    }
}
